<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" >

	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
	<link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css?family=Cousine|Inconsolata" rel="stylesheet">
	<link rel="shortcut icon" type="image/png" href="/favicon.png">

	
	<link rel="stylesheet" href="/css/style.css">
	<link rel="stylesheet" href="/css/css/fontawesome-all.min.css">
	<script src="/js/main.js"></script>
	

	<!-- Begin Jekyll SEO tag v2.1.0 -->
<title>PY: Using Keys Securely - Steem Developer</title>
<meta property="og:title" content="PY: Using Keys Securely" />
<meta name="description" content="In this tutorial we will learn how SteemPython library handles transaction signing with users key and how to securely manage your private keys" />
<meta property="og:description" content="In this tutorial we will learn how SteemPython library handles transaction signing with users key and how to securely manage your private keys" />
<link rel="canonical" href="https://developer.steem.io/tutorials-python/using_keys_securely" />
<meta property="og:url" content="https://developer.steem.io/tutorials-python/using_keys_securely" />
<meta property="og:site_name" content="Steem Developer" />
<meta property="og:type" content="article" />
<meta property="article:published_time" content="2018-07-20T12:56:08-05:00" />
<script type="application/ld+json">
{"@context": "http://schema.org",
"@type": "BlogPosting",
"headline": "PY: Using Keys Securely",
"datePublished": "2018-07-20T12:56:08-05:00",
"description": "In this tutorial we will learn how SteemPython library handles transaction signing with users key and how to securely manage your private keys",
"url": "https://developer.steem.io/tutorials-python/using_keys_securely"}</script>
<!-- End Jekyll SEO tag -->


	
</head>
<body>
<header>
	<h1>
		<button type="button" class="open-nav" id="open-nav"></button>
		<a href="/"  class="logo-link"><img src="/images/logotype_white.svg" height="40" alt="Steem Developer logo"></a>
	</h1>

	
	<form action="/search/" method="get">
		<input type="text" name="q" id="search-input" placeholder="Search">
		<input type="submit" value="Search" style="display: none;">
	</form>
	

	<div id="sidebar" class="sidebar">
	
	
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/">
		<h6 class="ctrl-nav-section title">Introduction</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/#introduction-welcome">Welcome to Steem</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/quickstart/">
		<h6 class="ctrl-nav-section title">Quickstart</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/quickstart/#quickstart-choose-library">Choose Library</a></li>
						
					
						
						<li><a href="/quickstart/#quickstart-steemd-nodes">steemd Nodes</a></li>
						
					
						
						<li><a href="/quickstart/#quickstart-testnet">Steem Testnet</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/tutorials/">
		<h6 class="ctrl-nav-section title">Tutorials</h6>
		<ul class="cont-nav-section content">
			
				
					
						<li><a href="/tutorials/#tutorials-javascript">Javascript</a></li>
					
				
					
						<li><a href="/tutorials/#tutorials-python">Python</a></li>
					
				
					
						<li><a href="/tutorials/#tutorials-ruby">Ruby</a></li>
					
				
					
						<li><a href="/tutorials/#tutorials-recipes">Recipes</a></li>
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/services/">
		<h6 class="ctrl-nav-section title">Services</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/services/#services-steemit">Steemit.com</a></li>
						
					
						
						<li><a href="/services/#services-steemconnect">SteemConnect</a></li>
						
					
						
						<li><a href="/services/#services-jussi">jussi</a></li>
						
					
						
						<li><a href="/services/#services-imagehoster">ImageHoster</a></li>
						
					
						
						<li><a href="/services/#services-sbds">SBDS</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/apidefinitions/">
		<h6 class="ctrl-nav-section title">Appbase API</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/apidefinitions/#apidefinitions-condenser-api">Condenser Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-account-by-key-api">Account By Key Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-broadcast-ops-comment">Broadcast Ops Comment</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-account-history-api">Account History Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-database-api">Database Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-follow-api">Follow Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-jsonrpc">Jsonrpc</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-market-history-api">Market History Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-network-broadcast-api">Network Broadcast Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-tags-api">Tags Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-witness-api">Witness Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-block-api">Block Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-broadcast-ops">Broadcast Ops</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/community/">
		<h6 class="ctrl-nav-section title">Community</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/community/#community-overview">Overview</a></li>
						
					
						
						<li><a href="/community/#community-resources">Resources</a></li>
						
					
						
						<li><a href="/community/#community-developeradvocate">Sharing & Help</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/glossary/">
		<h6 class="ctrl-nav-section title">Glossary</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/glossary/#glossary-chain-basics">Chain Basics</a></li>
						
					
						
					
						
						<li><a href="/glossary/#glossary-governance">Governance</a></li>
						
					
						
						<li><a href="/glossary/#glossary-transactions">Transactions</a></li>
						
					
						
						<li><a href="/glossary/#glossary-api">API</a></li>
						
					
						
						<li><a href="/glossary/#glossary-market">Market</a></li>
						
					
				
			
		</ul>
	</section>
	
</div>


	<!--<p class="copyright">-->
	<!--<a href="https://steemit.com.com/">-->
	<!--steemit-->
	<!--</a>-->
	<!--</p>-->
	<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
</header>
<div class="main" id="top">
	<div class="hero">
		<h1 class="hero__h1">Steem Developer Portal </h1>
		
		<img class="hero__img" src="/images/hero.png">
		
	</div>
	<section id="tutorials-python-using-keys-securely" class="row tutorials-python-using-keys-securely">
	<h3>
		<a id="tutorials-python-using-keys-securely">
			PY: Using Keys Securely
			
		</a>
	</h3>
	
		<p class="description">In this tutorial we will learn how SteemPython library handles transaction signing with users key and how to securely manage your private keys</p>
	

	<p><span class="fa-pull-left top-of-tutorial-repo-link"><span class="first-word">Full</span>, runnable src of <a href="https://github.com/steemit/devportal-tutorials-py/tree/master/tutorials/12_using_keys_securely">Using Keys Securely</a> can be downloaded as part of the <a href="https://github.com/steemit/devportal-tutorials-py">PY tutorials repository</a>.</span>
<br /></p>

<h2 id="intro">Intro</h2>

<p>All python tutorials will be in Python3 and developers following these tutorials should have sufficient knowlegde to use pip/pip3 package manager. Your development environment should be set up properly to test these tutorials.</p>

<p>Steem python library has 2 ways to handle your keys. One is from source code, another one is through command line interface called <code class="highlighter-rouge">steempy</code>. <code class="highlighter-rouge">steempy</code> cli is installed by default when you install steem-python library on your machine.</p>

<h2 id="steps">Steps</h2>

<ol>
  <li><a href="#app-setup"><strong>App setup</strong></a> - Library install and import</li>
  <li><a href="#example-list"><strong>Key usage example</strong></a> - Example showing how to import keys</li>
</ol>

<h4 id="1-app-setup-">1. App setup <a name="app-setup"></a></h4>

<p>In this tutorial we are only using <code class="highlighter-rouge">steem</code> package - steem-python library.</p>

<div class="language-python highlighter-rouge"><pre class="highlight"><code>  <span class="c"># initialize Steem class</span>
  <span class="kn">from</span> <span class="nn">steem</span> <span class="kn">import</span> <span class="n">Steem</span>

  <span class="c"># defining private keys inside source code is not secure way but possible</span>
  <span class="n">s</span> <span class="o">=</span> <span class="n">Steem</span><span class="p">(</span><span class="n">keys</span><span class="o">=</span><span class="p">[</span><span class="s">'&lt;private_posting_key&gt;'</span><span class="p">,</span> <span class="s">'&lt;private_active_key&gt;'</span><span class="p">])</span>
</code></pre>
</div>

<p>Last line from above snippet shows how to define private keys for account that’s going to transact using script.</p>

<h4 id="2-key-usage-example-">2. Key usage example <a name="example-list"></a></h4>

<p>After defining private keys inside Steem class, we can quickly sign any transaction and broadcast it to the network.</p>

<div class="language-python highlighter-rouge"><pre class="highlight"><code>  <span class="c"># above will allow accessing Commit methods such as</span>
  <span class="c"># demo account sending 0.001 STEEM to demo1 account</span>

  <span class="n">s</span><span class="o">.</span><span class="n">commit</span><span class="o">.</span><span class="n">transfer</span><span class="p">(</span><span class="s">'demo'</span><span class="p">,</span><span class="s">'0.001'</span><span class="p">,</span><span class="s">'STEEM'</span><span class="p">,</span><span class="s">'memo text'</span><span class="p">,</span><span class="s">'demo1'</span><span class="p">)</span>
</code></pre>
</div>

<p>Above method works but it is not secure way of handling your keys because you have entered your keys within source code that you might leak accidentally. To avoid that, we can use CLI - command line interface <code class="highlighter-rouge">steempy</code>.</p>

<p>You can type following to learn more about <code class="highlighter-rouge">steempy</code> commands:</p>

<div class="language-python highlighter-rouge"><pre class="highlight"><code>  <span class="n">steempy</span> <span class="o">-</span><span class="n">h</span>
</code></pre>
</div>

<p><code class="highlighter-rouge">steempy</code> lets you leverage your <a href="https://bitcoinpaperwallet.com/bip38-password-encrypted-wallets/">BIP38</a> encrypted wallet to perform various actions on your accounts.</p>

<p>The first time you use steempy, you will be prompted to enter a password. This password will be used to encrypt the steempy wallet, which contains your private keys.</p>

<p>You can import your Steem username with following command:</p>

<p><code class="highlighter-rouge">steempy importaccount username</code></p>

<p>Next you can import individual private keys:</p>

<p><code class="highlighter-rouge">steempy addkey &lt;private_key&gt;</code></p>

<p>That’s it, now that your keys are securely stored on your local machine, you can easily sign transaction from any of your Python scripts by using defined keys.</p>

<div class="language-python highlighter-rouge"><pre class="highlight"><code>  <span class="c"># if private keys are not defined</span>
  <span class="c"># accessing Wallet methods are also possible and secure way</span>
  <span class="n">s</span><span class="o">.</span><span class="n">wallet</span><span class="o">.</span><span class="n">get_active_key_for_account</span><span class="p">(</span><span class="s">'demo'</span><span class="p">)</span>
</code></pre>
</div>

<p>Above line fetches private key for user <code class="highlighter-rouge">demo</code> from local machine and signs transaction.</p>

<p><code class="highlighter-rouge">steempy</code> also allows you to sign and broadcast transactions from terminal. For example:</p>

<p><code class="highlighter-rouge">steempy transfer --account &lt;account_name&gt; &lt;recipient_name&gt; 1 STEEM memo</code></p>

<p>would sign and broadcast transfer operation,</p>

<p><code class="highlighter-rouge">steempy upvote --account &lt;account_name&gt; link</code></p>

<p>would sing and broadcast vote operation, etc.</p>

<p>That’s it!</p>

<h3 id="to-run-the-tutorial">To Run the tutorial</h3>

<ol>
  <li>clone this repo</li>
  <li><code class="highlighter-rouge">cd tutorials/12_using_keys_securely</code></li>
  <li><code class="highlighter-rouge">pip install -r requirements.txt</code></li>
  <li><code class="highlighter-rouge">python index.py</code></li>
  <li>After a few moments, you should see output in terminal/command prompt screen.</li>
</ol>

<hr />

</section>

</div>
<div class="footer">
	<a href="#">Back to top</a>
</div>
<script>
    document.getElementById("open-nav").addEventListener("click", function () {
        document.body.classList.toggle("nav-open");
    });
</script>
</body>
</html>


<script src="/js/adjust.js"></script>

